package com.kaili.basic.controller.web.v1;


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseParameterControlAddDto;
import com.kaili.basic.pojo.dto.BaseParameterControlQueryDto;
import com.kaili.basic.pojo.entity.BaseParameterControl;
import com.kaili.basic.pojo.vo.BaseParameterControlVO;
import com.kaili.basic.service.IBaseParameterControlService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.CommonUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 参数控制 前端控制器
 * </p>
 *
 * @author nigang
 * @since 2018-06-28
 */
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "参数控制", description = "参数控制相关API")
public class BaseParameterControlController {
    @Autowired
    IBaseParameterControlService baseParameterControlService;

    @PostMapping(value = "/parameterControl")
    @ApiOperation(value = "保存参数控制", notes = "保存参数控制")
    public RestStatus saveParameterControl(@Validated @RequestBody BaseParameterControlAddDto dto){
        BaseParameterControl parameterControl=new BaseParameterControl();
        BeanUtils.copyProperties(dto,parameterControl);
        BaseParameterControl baseParameterControl = baseParameterControlService.getParameterControlBySiteUser(parameterControl);
        if(baseParameterControl!=null){
            parameterControl.setParameterId(baseParameterControl.getParameterId());
            baseParameterControlService.updateParameterControl(parameterControl);
            return CommonStatus.SAVE_OK;
        }else {
            Long loginUserId = LoginDetailUtils.getCurrentEmployeeId().longValue();
            if (parameterControl.getParameterId() == null) {
                parameterControl.setCreatedBy(loginUserId);
                parameterControl.setCreatedTime(new Date());
                if (baseParameterControlService.addParameterControl(parameterControl) > 0) {
                    return CommonStatus.SAVE_OK;
                }
            } else {
                parameterControl.setModifiedBy(loginUserId);
                parameterControl.setModifiedTime(new Date());
                if (baseParameterControlService.updateParameterControl(parameterControl) > 0) {
                    return CommonStatus.SAVE_OK;
                }
            }
        }
        return CommonStatus.SAVE_FAIL;
    }

    @DeleteMapping(value = "/parameterControl/{parameterId}")
    @ApiOperation(value = "删除参数控制", notes = "根据id删除参数控制")
    public RestStatus deleteParameterControlById(@ApiParam("参数id")@PathVariable("parameterId") Integer parameterId){
        baseParameterControlService.deleteParameterControlById(parameterId);
        return CommonStatus.DELETE_OK;
    }

    @GetMapping(value = "/parameterControl")
    @ApiOperation(value = "查所有参数控制", notes = "查所有参数控制")
    public Page<BaseParameterControlVO> getAllParameterControl(@ModelAttribute BaseParameterControlQueryDto  dto){
        BaseParameterControl parameterControl=new BaseParameterControl();
        BeanUtils.copyProperties(dto,parameterControl);
        return baseParameterControlService.getAllParameterControl(new Page<BaseParameterControl>(parameterControl.getPage(), parameterControl.getPageSize()), parameterControl);
    }

    @GetMapping(value = "/parameterControl/{parameterId}")
    @ApiOperation(value = "查参数控制", notes = "根据id查询参数控制")
    public BaseParameterControlVO getParameterControlById(@ApiParam("参数id")@PathVariable("parameterId") Integer parameterId){
        BaseParameterControlVO controlVO=new BaseParameterControlVO();
        BaseParameterControl baseParameterControl=baseParameterControlService.getParameterControlById(parameterId);
        BeanUtils.copyProperties(baseParameterControl,controlVO);
        return controlVO;
    }

    @GetMapping(value = "/parameterControl/bySiteUser")
    @ApiOperation(value = "查参数控制", notes = "根据登录的网点和用户查询参数控制")
    public BaseParameterControlVO getParameterControlBySiteUser(@ModelAttribute BaseParameterControlQueryDto dto){
        BaseParameterControl parameterControl=new BaseParameterControl();
        BeanUtils.copyProperties(dto,parameterControl);
        BaseParameterControl control = baseParameterControlService.getParameterControlBySiteUser(parameterControl);
        BaseParameterControlVO controlVO=new BaseParameterControlVO();
        if (control != null ) {
            if (control.getCollectionCharge() == null || control.getCollectionCharge() == 0) {
                control.setCollectionCharge(2.00);
            }
            if (control.getNormalGoodsPremiumRate() == null || control.getNormalGoodsPremiumRate() == 0) {
                control.setNormalGoodsPremiumRate(2.00);
            }
            if (control.getFragilePremiumRate() == null || control.getFragilePremiumRate() == 0) {
                control.setFragilePremiumRate(2.00);
            }
            BeanUtils.copyProperties(control,controlVO);
            return controlVO;
        }else {
            BaseParameterControl baseParameterControl = new BaseParameterControl();
            baseParameterControl.setCollectionCharge(2.00);
            baseParameterControl.setNormalGoodsPremiumRate(2.00);
            baseParameterControl.setFragilePremiumRate(2.00);
            BeanUtils.copyProperties(baseParameterControl,controlVO);
            return controlVO;
        }

    }

    @GetMapping(value = "/parameterControl/byCompany")
    @ApiOperation(value = "查参数控制", notes = "根据登录的网点和用户查询参数控制")
    public BaseParameterControlVO getParameterByCompany(){
        BaseParameterControlVO controlVO=new BaseParameterControlVO();
        BaseParameterControl control = baseParameterControlService.getParameterByCompany();
        if (control != null ) {
            if (control.getCollectionCharge() == null || control.getCollectionCharge() == 0) {
                control.setCollectionCharge(2.00);
            }
            if (control.getNormalGoodsPremiumRate() == null || control.getNormalGoodsPremiumRate() == 0) {
                control.setNormalGoodsPremiumRate(2.00);
            }
            if (control.getFragilePremiumRate() == null || control.getFragilePremiumRate() == 0) {
                control.setFragilePremiumRate(2.00);
            }
            BeanUtils.copyProperties(control,controlVO);
            return controlVO;
        }else {
            BaseParameterControl baseParameterControl = new BaseParameterControl();
            baseParameterControl.setCollectionCharge(2.00);
            baseParameterControl.setNormalGoodsPremiumRate(2.00);
            baseParameterControl.setFragilePremiumRate(2.00);
            BeanUtils.copyProperties(baseParameterControl,controlVO);
            return controlVO;
        }

    }
}

